package spitfire.ksim.algorithm;

import java.util.List;

import spitfire.ksim.data.TSDRequestData;
import spitfire.ksim.data.TSDRequestData.RequestEntry;

/**
 * Select the semantic description with the highest similarity score among 
 * all type of sensor in the node.
 *  
 * @author Adam
 */
public class RepresentativeSelection implements RequestAlgorithm {
	
	private LazyClassificationAlgorithm algo;
	
	public RepresentativeSelection(LazyClassificationAlgorithm algo) {
		this.algo = algo;
	}
	/**
	 * @see RequestAlgorithm#requestForSD(FuzzyRuleBase, KNodeSnapshot)
	 */
	@Override
	public String requestForSD(List<FuzzyEntry> fuzzyEntryList, 
			TSDRequestData requestData) {
		System.out.println("Requesting with Representative");
		String maxSD = null;
		double scoreMax = 0;
		
		List<RequestEntry> requestEntryList = requestData.getData();
		// For each sensor type
		for (RequestEntry requestEntry : requestEntryList) {
			String sensorType = requestEntry.sensorType;
			List<Long> timeStampList = requestEntry.sensorSnapshot.getTimeStampList();
			List<Double> dataList = requestEntry.sensorSnapshot.getDataList();

			System.out.println("Calculate score for <" + sensorType + ">");

			String sd = algo.classifySD(timeStampList, dataList, fuzzyEntryList);
			double sc = algo.getSimilarity();
			System.out.println("Similarity to <" + sd + ">: " + sc);
			if (sc > scoreMax) {
				scoreMax = sc;
				maxSD = sd;
			}
		}
		
		if (maxSD == null) {
			System.out.println("No SD selected");
		} else {
			System.out.println("<" + maxSD + "> selected with score: " + scoreMax);
		}
		return maxSD;
	}
}
